Ext.namespace('Divo');
Ext.namespace('Divo.app');
Divo.app.checkGrid = Ext.extend(Ext.grid.EditorGridPanel, {
      initComponent : function() {
        var me = this;
        var d = new Date();
        var year = d.getYear().toString();
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        var time;
        if (month.length > 1) {
          time = year + month;
        } else {
          time = year + "0" + month;
        }

        if (day.length > 1) {
          time = time + day;
        } else {
          time = time + "0" + day;
        }
        var readerRecord = Ext.data.Record.create([{
              name : "id",
              type : "float"
            }, {
              name : "name",
              type : "string"
            }, {
              name : "factoryCode",
              type : "string"
            }, {
              name : "materialId",
              type : "float"
            }, {
              name : "supplierId",
              type : "float"
            }, {
              name : "supplierName",
              type : "string"
            }, {
              name : "materialName",
              type : "string"
            }, {
              name : "spec",
              type : "string"
            }, {
              name : "consum",
              type : "string"
            }, {
              name : "unit",
              type : "string"
            }, {
              name : "managerCode",
              type : "string"
            }, {
              name : "regNo",
              type : "string"
            }, {
              name : "readyCodeState",
              type : "string"
            }, {
              name : "endTime",
              type : "string"
            }, {
              name : "state",
              type : "string"
            }]);

        var ds = new Ext.data.Store({
              proxy : new Ext.data.HttpProxy({
                    url : 'ready_code/query_ready_code'
                  }),
              reader : new Ext.data.JsonReader({
                    root : 'rows',
                    totalProperty : 'totalCount',
                    id : 'id'
                  }, readerRecord)
            });
        var cm = new Ext.grid.ColumnModel({
              columns : [{
                    header : '',
                    dataIndex : 'id',
                    hidden : true
                  }, {
                    header : '品牌名',
                    dataIndex : 'name',
                    width : 100
                  }, {
                    header : '<font color="blue">厂家码</font>',
                    dataIndex : 'factoryCode',
                    editor : new app.base.NumberField({
                          name : "factoryCode",
                          dataIndex : "factoryCode",
                          allowBlank : false,
                          width : 100,
                          insert_allowed : true,
                          update_allowed : true
                        })
                  }, {
                    header : '供应商',
                    dataIndex : 'supplierName',
                    width : 100
                  }, {
                    header : '厂家耗材名',
                    dataIndex : 'materialName',
                    width : 100
                  }, {
                    header : '规格',
                    dataIndex : 'spec',
                    width : 100
                  }, {
                    header : '对应消耗品',
                    dataIndex : 'consum',
                    width : 100
                  }, {
                    header : '单位',
                    dataIndex : 'unit',
                    width : 100
                  }, {
                    header : '管理码',
                    dataIndex : 'managerCode',
                    width : 100
                  }, {
                    header : '营业执照注册号',
                    dataIndex : 'regNo',
                    width : 100
                  }, {
                    header : '材料状态',
                    dataIndex : 'readyCodeState',
                    width : 100,
                    renderer : function(val, m) {
                      if (val == "0") {
                        return "备码";
                      }

                      if (val == "1") {
                        return "备货";
                      }

                      if (val == "2") {
                        return "取消备货";
                      }

                      if (val == "3") {
                        return "配发";
                      }

                      if (val == "4") {
                        return "已划价";
                      }

                      if (val == "5") {
                        return "已入库";
                      }
                    }
                  }, {
                    header : '有效日期',
                    dataIndex : 'endTime',
                    width : 100
                  }, {
                    header : '有效状态',
                    dataIndex : 'state',
                    width : 100,
                    renderer : function(val, m) {
                      var endTime = m.value;
                      var year = endTime.substring(0, 4);
                      var month = endTime.substring(5, 7);
                      var day = endTime.substring(8, 10);
                      var endTimeStr = year + month + day;
                      if (parseInt(endTimeStr) - parseInt(time) >= 0) {
                        return "OK";
                      } else {
                        return "NO";
                      }
                    }
                  }, {
                    dataIndex : 'materialId',
                    hidden : true
                  }, {
                    dataIndex : 'supplierId',
                    hidden : true
                  }]
            });
        Ext.apply(this, {
              store : ds,
              cm : cm,
              loadMask : true,
              clicksToEdit : 1,
              listeners : {
                afteredit : function(cm, colIndex, width) {
                  var id = cm.record.get("id");
                  var factoryCode = cm.record.get("factoryCode");
                  var me = this;
                  Ext.Ajax.request({
                        params : {
                          id : id,
                          factoryCode : factoryCode
                        },
                        url : "ready_code/ready_goods",
                        callback : function(o, s, r) {
                          var respText = Ext.decode(r.responseText);
                          if (respText.success) {
                            if (cm.row + 1 < ds.getCount())
                              me.startEditing(cm.row + 1, cm.column);
                          } else {
                            if (respText.message.length > 0) {
                              this.startEditing(cm.row, cm.column);
                              Ext.Msg.alert('错误', respText.message);
                            }
                          }
                        }
                      });
                }
              }
            });
        Divo.app.checkGrid.superclass.initComponent.apply(this, arguments);
      },
      onRender : function() {
        Divo.app.checkGrid.superclass.onRender.apply(this, arguments);
      },
      reloadRecords : function() {
        this.getStore().reload();
      }
    });

Divo.app.ReadyGoodsList = function() {
  /* ----------------------- private属性 ----------------------- */
  var gridEl = 'ReadyCode-grid';
  var grid, form, selModel, toolbar, win;
  var data, sId;
  var fldTime, fldCode;
  /* ----------------------- private方法 ----------------------- */
  // 创建列表
  function createGrid() {
    var queryFlds = new Array();
    var searchFlds = new Ext.util.MixedCollection();
    queryFlds.push('name');
    var fldName = new Ext.form.TextField({
          name : 'name',
          id : gridEl + '-name',
          fieldLabel : '名称',
          allowBlank : true,
          width : 100

        });
    searchFlds.add('name', fldName);

    grid = new Divo.app.checkGrid({
          remoteSort : true,
          gridId : gridEl + "check",
          queryFlds : queryFlds,
          searchFlds : searchFlds,
          hasBbar : false,
          jsonId : 'id',
          recordPk : ["id"]
        });
  }

  // 刷新列表
  function reloadRecords() {
    grid.getStore().reload();
  }

  // 创建layout
  function createLayout() {
    toolbar = new Ext.Toolbar({
          items : [{
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESH",
                handler : reloadRecords
              }]
        });
    var viewport = new Ext.Viewport({
          layout : 'border',
          items : [{
                layout : 'border',
                region : 'center',
                border : false,
                items : [{
                      layout : 'fit',
                      region : 'north',
                      border : false,
                      items : [toolbar],
                      height : 25
                    }, {
                      layout : 'fit',
                      region : 'center',
                      border : false,
                      items : [grid]
                    }]
              }]
        });
    viewport.doLayout();
  }

  function onLoad() {
    var store = grid.getStore();
    if (store.getCount() > 0)
      grid.startEditing(0, 2);
  }

  /* ----------------------- public方法 ----------------------- */
  return {
    /**
     * 初始化
     */
    init : function() {
      createGrid();
      createLayout();
      grid.getStore().load();
      grid.getStore().on("load", onLoad);
    }
  }
}();

Ext.onReady(Divo.app.ReadyGoodsList.init, Divo.app.ReadyGoodsList, true);